<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 支微
 */
class Zhiwei1Controller extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));

        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $isMobile = isMobile();

        $return = $this->orderadd( $parameter );

        $data = array();
        switch($this->config['paytype']){
            //网银直连
            case 6:
                $data['transCode'] = '003';
                $data['service'] = '0012';
                switch($product['code']){
                    //北京银行
                    case 'BOB':
                        $data['bankCode'] = 'BCCB';
                        break;
                    //东亚银行
                    case 'BEA':
                        $data['bankCode'] = 'BEA';
                        break;
                    //中国工商银行
                    case 'ICBC':
                        $data['bankCode'] = 'ICBC';
                        break;
                    //光大银行
                    case 'CEB':
                        $data['bankCode'] = 'CEB';
                        break;
                    //广发银行
                    case 'CGB':
                        $data['bankCode'] = 'CGB';
                        break;
                    //华夏银行
                    case 'HXB':
                        $this->showmessage('暂不支持此银行卡!',['code'=>C("THIRDCODES.".$this->class)]);
                        break;
                    //中国建设银行
                    case 'CCB':
                        $data['bankCode'] = 'CCB';
                        break;
                    //交通银行
                    case 'BOCOM':
                        $data['bankCode'] = 'BOCOM';
                        break;
                    //中国民生银行
                    case 'CMBC':
                        $data['bankCode'] = 'CMBC';
                        break;
                    //南京银行
                    case 'NJCB':
                        $data['bankCode'] = 'NJCB';
                        break;
                    //宁波银行
                    case 'NBCB':
                        $data['bankCode'] = 'NBCB';
                        break;
                    //中国农业银行
                    case 'ABC':
                        $data['bankCode'] = 'ABC';
                        break;
                    //平安银行
                    case 'PAB':
                        $data['bankCode'] = 'PINGAN';
                        break;
                    //上海银行
                    case 'SHB':
                        $data['bankCode'] = 'SRCB';
                        break;
                    //上海浦东发展银行
                    case 'SPDB':
                        $data['bankCode'] = 'SPDB';
                        break;
                    //深圳发展银行
                    case 'SDB':
                        $this->showmessage('暂不支持此银行卡!',['code'=>C("THIRDCODES.".$this->class)]);
                        break;
                    //兴业银行
                    case 'CIB':
                        $data['bankCode'] = 'CIB';
                        break;
                    //中国邮政储蓄银行
                    case 'PSBS':
                        $data['bankCode'] = 'POST';
                        break;
                    //招商银行
                    case 'CMB':
                        $data['bankCode'] = 'CMB';
                        break;
                    //浙商银行
                    case 'CZB':
                        $this->showmessage('暂不支持此银行卡!',['code'=>C("THIRDCODES.".$this->class)]);
                        break;
                    //中国银行
                    case 'BOC':
                        $data['bankCode'] = 'BOC';
                        break;
                    //中信银行
                    case 'CTTIC':
                        $data['bankCode'] = 'CITIC';
                        break;
                    default:
                        $this->showmessage('暂不支持此银行卡!',['code'=>C("THIRDCODES.".$this->class)]);
                }
                break;
            default:
                $this->showmessage('此通道暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }
        $data['customerNo'] = strval($this->config['mch_id']);
        $data['externalId'] = strval($return['orderid']);
        $data['transAmount'] = strval(intval($return['amount']*100));
        $data['description'] = 'ipa';
        $data['reqDate'] = date("Ymd",time());
        $data['reqTime'] = date("His",time());
        $data['callbackUrl'] = $return['callbackurl'];
        $data['bgReturnUrl'] = $return['notifyurl'];
        $data['selfData'] = 'wlf';
        $data['cardType'] = '1';
        //$data['requestIp'] = $_SERVER['SERVER_ADDR'];
        $data['requestIp'] = $_SERVER['HTTP_SLB_IP'];
        $data['terminalType'] = '1';
        if(in_array($product['code'],['CCB'])){
            if($isMobile){
                $data['terminalType'] = '2';
            }
        }
        $data['sign'] = $this->getSign($data);

        $res = curl_json($this->config['gateway'],$data);
        $res = json_decode($res, true);
        if(!$res || empty($res)){
            $this->showmessage('未获取到数据!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $code = $res['payUrl'];
        if(!$code){
            if($res['message']){
                $this->showmessage($res['message'],['code'=>C("THIRDCODES.".$this->class)]);
            }

            $this->showmessage('此通道暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        switch($this->config['paytype']){
            case 6:
                echo $code; exit;
            default:
        }
    }

    private function getSign($data){
        $r = [];

        foreach($data as $k=>$v){
            if($k === 'sign' || $v === ''){
                continue;
            }

            $r[$k] = $v;
        }

        ksort($r);

        $str = httpstr($r).'&key='.$this->config['signkey'];

        return strtoupper(md5($str));
    }

    //通知
    public function notifyurl(){
        //file_put_contents("a.txt",var_export($_POST,true)."\r\n\r\n");
        //file_put_contents("a.txt",var_export($_GET,true)."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", file_get_contents('php://input')."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", date("Y-m-d H:i:s",time())."\r\n\r\n", FILE_APPEND);

        $p = $origin = file_get_contents('php://input');

        //$p = '{"sign":"8A308060844EA13B3B4353F5CCDE840D","amount":"1000","message":"success","seqno":"20181219202661030427","customerNo":"289300000004","code":"00","selfData":"wlf","externalId":"2018121910490156524"}';

        $p = json_decode($p, true);

        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['externalId'];
        $find = M("Order")->where(array("pay_orderid"=>$orderid))->find();
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "server_return"=> $origin,
            "pay_orderid_rd"=>$p['seqno']
        ));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];
        $sign2 = $this->getSign($p);

        if($sign !== $sign2){
            exit("sign_wrong");
        }

        if($p['code'] !== '00'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],'SUCCESS');
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}